<?php
/**
 * @version		$Id: vinaora_param.php 2011-07-05 vinaora $
 * @package		VINAORA SLICK SLIDESHOW
 * @subpackage	mod_vinaora_slickshow
 * @copyright	Copyright (C) 2010 - 2011 VINAORA. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 * @website		http://vinaora.com
 * @twitter		http://twitter.com/vinaora
 * @facebook	http://facebook.com/vinaora
 */

// no direct access
defined('_JEXEC') or die;

class extVinaoraParam
{

	/*
	 * Get a Parameter in a Parameters String which are separated with a specify symbol (default: vertical bar '|').
	 * Example: Parameters = "value1 | value2 | value3". Return "value2" if positon = 2
	 */
	public static function getParam($params, $position=1, $symbol='|'){

		$return = NULL;

		$items = explode($symbol, $params);

		if ( ($position > count($items)) || ($position<1) ) return NULL;
		else {
			$return = $items[$position-1];
		}

		return $return;
	}
	
	
	/*
	 * Valid Link Target
	 * Return: _blank, _parent, _self, _top
	 */
	public static function validTarget($target, $default='_blank'){
		$target = strtolower( trim($target) );
		
		// Add '_' symbol to the beginning of string if not exist
		$target = "_" . ltrim($target, "_");

		$valid = array ('_blank', '_parent', '_self', '_top');
		$target = in_array($target, $valid) ? $target : $default;
		
		return $target;
	}
	
	/*
	 * Valid Color
	 */
	public static function validColor($color, $default="ffffff", $prefix=""){
		$color = strtolower ( trim($color) );
		
		if (empty($color)) return $prefix.$default;
		
		// Remove '0x' from the beginning of string if exist
		$color = ltrim($color, "0x") ;
		
		// Remove '#' from the beginning of string if exist
		$color = ltrim($color, "#") ;
		
		// The length of string less than or equal 6
		$color = substr($color, 0, 6);
		
		$valid = true;
		$hexa = '0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f';
		$hexa = explode(",", $hexa);

		// TODO: check the length of $color
		
		for($i=0; $i<strlen($color); $i++){
			if(!in_array($color[$i], $hexa)){
				$valid = false;
				break;
			}
		}
		
		$color = ($valid === false) ? $default : $color;
		
		$color = $prefix.$color;
		
		return $color;
	}
	
	/*
	 * Get Random Color
	 */
	public static function rand_color() {
		return substr('00000' . dechex(mt_rand(0, 0xffffff)), -6);
	}
	
	/*
	 * Valid Transparency (From 0-100)
	 */
	public static function validTransparency($t, $default=0){
		$t = intval($t);
		$t = min($t, 100);
		$t = max($t, 0);
		
		return $t;
	}
	
	/*
	 * Add SWFObject Library to <head> tag
	 */
	public static function addSWFObject($swfobject){

		switch($swfobject){

			// Use Local file
			case 'i2.0':
			case 'i2.1':
			case 'i2.2':
				$version = substr($swfobject, 1);
				$path = MEDIA_URL.'js/swfobject/'.$version.'/';
				JHTML::script('swfobject.js', $path);
				break;

			// Use Google Hosting
			case 'e2.1':
			case 'e2.2':
				$version = substr($swfobject, 1);
				$path = AJAX_SWFOBJECT_URL.$version.'/';
				JHTML::script('swfobject.js', $path);
				break;

			case '0':
			default:
				return;
				break;
		}

	}
	
	/*
	 * Valid Params
	 */
	public static function &validParams(&$params){
	
		$param = htmlspecialchars( trim($params->get( 'moduleclass_sfx' )) );
		$params->set('moduleclass_sfx', $param);
	
		$param = intval($params->get('width'));
		$param = abs($param);
		$params->set('width', $param);
		
		$param = intval($params->get('height'));
		$param = abs($param);
		$params->set('height', $param);
		
		$param = intval($params->get('startWith'));
		$param = abs($param);
		$params->set('startWith', $param);
		
		$param = $params->get('backgroundColor');
		$param = self::validColor($param, "0x");
		$params->set('backgroundColor', $param);
		
		$param = $params->get('backgroundTransparency');
		$param = self::validTransparency($param);
		$params->set('backgroundTransparency', $param);
		
		$param = intval($params->get('barHeight'));
		$param = abs($param);		
		$params->set('barHeight', $param);
		
		return $params;
	}
	
	/*
	 * Get the Base Path
	 */
	public static function getBasePath($relative = true){
		// Use JURI::base() for Full URL
		$path = JURI::base($relative);
		
		// Add slash symbol "/" to the end of path
		$path = rtrim($path, "/") . "/";
		
		return $path;
	}

}

